package com.kzd.btgame.main.mine.adapter

import android.widget.CheckBox
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.kzd.btgame.R
import com.kzd.btgame.entity.*
import com.kzd.btgame.main.game.adapter.GameListLabelAdapter
import com.kzd.btgame.main.toMoneyMallDetActivity
import com.module.lemlin.image.loadCenterImage
import com.module.lemlin.image.loadImage
import java.lang.StringBuilder

class MineGiftAdapter : BaseQuickAdapter<GameGift, BaseViewHolder>(R.layout.item_giving_list) {
    init {
        addChildClickViewIds(R.id.tvGameGiftCopy)
    }

    override fun convert(holder: BaseViewHolder, item: GameGift) {
        holder.setText(R.id.tvGameGiftName, item.name)
            .setText(R.id.tvGameGiftContent, StringBuilder().append("礼包内容：").append(item.content))
            .setText(R.id.tvGameGiftCode, StringBuilder().append("礼包码：").append(item.card_no))
            .getView<ImageView>(R.id.ivGameGiftIcon)
            .loadImage(item.icon)
    }
}

class FeedbackTypeAdapter :
    BaseQuickAdapter<FeedbackType, BaseViewHolder>(R.layout.item_feedback_type) {
    override fun convert(holder: BaseViewHolder, item: FeedbackType) {
        holder.getView<CheckBox>(R.id.cbFeedbackTypeName).apply {
            isChecked = item.isSelect
        }.text = item.name

        holder.getView<CheckBox>(R.id.cbFeedbackTypeName)
            .setOnCheckedChangeListener { _, isChecked ->
                item.isSelect = isChecked
            }
    }
}

class MemberMoneyCardAdapter :
    BaseQuickAdapter<MoneySaving, BaseViewHolder>(R.layout.item_member_money_card) {

    var selectPosition = 0
        set(value) {
            if (field != value) {
                field = value
                notifyDataSetChanged()
            }
        }

    override fun convert(holder: BaseViewHolder, item: MoneySaving) {

//        val price = if (item.first_charge == 1) {
//            StringBuilder().append("¥").append(item.first_money).append(item.pay_money)
//        } else StringBuilder().append("¥").append(item.pay_money)
//
//        SpannableStringBuilder().apply {
//            val pay_money = item.pay_money.toString()
//            val first_money = item.first_money.toString()
//            if (item.first_charge == 1) {
//                append("¥").append(first_money).append(pay_money)
//            } else {
//                append("¥").append(pay_money)
//            }
//            setSpan(
//                RelativeSizeSpan(0.5f), 0, 1,
//                Spannable.SPAN_INCLUSIVE_EXCLUSIVE
//            )
//            if (item.first_charge != 1)
//                return
//            setSpan()
//        }

        holder.setText(R.id.tvMemberMoneyCardName, item.name)
            .setText(
                R.id.tvMemberMoneyCardMoney,
                if (item.first_charge == 1) item.first_money.toString() else item.pay_money.toString()
            )
            .setText(
                R.id.tvMemberMoneyCardMoney1,
                if (item.first_charge == 1) StringBuilder().append("原价¥")
                    .append(item.pay_money.toString()) else null
            )
            .setText(R.id.tvMemberMoneyCardWorth, StringBuilder().append("价值¥").append(item.money))
            .setGone(R.id.tvMemberMoneyCardMoney1, (item.first_charge != 1))
            .setGone(R.id.clMemberMoneyCardFirst, (item.first_charge != 1))

        holder.getView<ImageView>(R.id.ivMemberMoneyCardSelect).apply {
            if (selectPosition == holder.layoutPosition) {
                setImageResource(R.mipmap.ic_70004)
            } else {
                setImageResource(R.mipmap.ic_70003)
            }
        }
    }
}

class MemberCenterCardAdapter :
    BaseQuickAdapter<CenterConfig, BaseViewHolder>(R.layout.item_member_center_card) {

    var selectPosition = 0
        set(value) {
            if (field != value) {
                field = value
                notifyDataSetChanged()
            }
        }

    override fun convert(holder: BaseViewHolder, item: CenterConfig) {
        holder.setText(R.id.tvMemberMoneyCardText, item.text1)
            .setText(R.id.tvMemberMoneyCardMoney, item.money)
            .setText(R.id.tvMemberMoneyCardName, item.name)
            .setText(
                R.id.tvMemberMoneyCardWorth,
                StringBuilder().append("返").append(item.zeng).append("平台币")
            )

        holder.getView<ImageView>(R.id.ivMemberMoneyCardSelect).apply {
            if (selectPosition == holder.layoutPosition) {
                setImageResource(R.mipmap.ic_70004)
            } else {
                setImageResource(R.mipmap.ic_70003)
            }
        }
    }
}

class MemberCenterContentAdapter :
    BaseQuickAdapter<CenterContent, BaseViewHolder>(R.layout.item_member_center_content) {
    override fun convert(holder: BaseViewHolder, item: CenterContent) {
        holder.setText(R.id.tvCenterContentName, item.title)
            .setText(R.id.tvCenterContentDesc, item.content)
            .getView<ImageView>(R.id.ivCenterContentIcon)
            .loadImage(item.img)
    }
}

class MallCollectAdapter :
    BaseQuickAdapter<MallGoods, BaseViewHolder>(R.layout.item_mall_collect) {

    init {
        setOnItemClickListener { _, _, position ->
            val itemData = getItem(position)
            toMoneyMallDetActivity(itemData.goods_id)
        }
    }

    override fun convert(holder: BaseViewHolder, item: MallGoods) {
        holder.setText(R.id.tvMallCollectName, item.goods_name)
            .setText(
                R.id.tvMallCollectPoint,
                StringBuilder().append(item.market_price).append("积分")
            )
            .getView<ImageView>(R.id.ivMallCollectIcon)
            .loadImage(item.goods_thumb)
    }
}

class MineDiscussAdapter :
    BaseQuickAdapter<MineDiscuss, BaseViewHolder>(R.layout.item_game_discuss_mine) {
    override fun convert(holder: BaseViewHolder, item: MineDiscuss) {
        holder.setText(R.id.tvGameDiscussMineName, item.name)
            .setText(R.id.tvGameDiscussMineContent, item.content)
            .setText(R.id.tvGameDiscussMineTime, item.time)
            .setText(R.id.tvGameDiscussMineType, item.type)
            .getView<ImageView>(R.id.ivGameDiscussMineIcon)
            .loadCenterImage(item.icon)

        holder.getView<RecyclerView>(R.id.rvGameDiscussMineLabelBody)
            .adapter = GameListLabelAdapter().apply {
            setNewInstance(item.biaoqian.toMutableList())
        }
    }
}

class MineRecordBillAdapter :
    BaseQuickAdapter<MineOrder, BaseViewHolder>(R.layout.item_record_bill) {
    override fun convert(holder: BaseViewHolder, item: MineOrder) {
        holder.setText(R.id.tvRecordBillTime, item.time.replace(" ", "\n"))
            .setText(
                R.id.tvRecordBillPoint,
                if (item.points.toInt() < 0) item.points else "+${item.points}"
            )
            .setText(R.id.tvRecordBillName, item.type_name)
    }
}

class MineRecordLotteryAdapter :
    BaseQuickAdapter<MineLottery, BaseViewHolder>(R.layout.item_record_bill) {
    override fun convert(holder: BaseViewHolder, item: MineLottery) {
        holder.setText(R.id.tvRecordBillTime, item.time.replace(" ", "\n"))
            .setText(R.id.tvRecordBillPoint, item.gold)
            .setText(R.id.tvRecordBillName, item.note)
    }
}

class MineMessageAdapter :
    BaseQuickAdapter<UserMessage, BaseViewHolder>(R.layout.item_message_list) {
    override fun convert(holder: BaseViewHolder, item: UserMessage) {
        holder.setText(R.id.tvMessageListType, item.type)
            .setText(R.id.tvMessageListTime, item.time)
            .setText(R.id.tvMessageListName, item.name)
    }
}

class MineAddressAdapter :
    BaseQuickAdapter<UserAddress, BaseViewHolder>(R.layout.item_address_list) {
    init {
        addChildClickViewIds(R.id.ivAddressEdit)
    }
    override fun convert(holder: BaseViewHolder, item: UserAddress) {
        holder.setText(R.id.tvAddressName, item.consignee)
            .setText(R.id.tvAddressPhone, item.mobile)
            .setText(R.id.tvAddressDetails, item.uaddress)
            .setGone(R.id.tvAddressIsDefault, (item.moren == "0"))
    }
}